package com.maxd.modules.coupon.controller;

import com.gexin.fastjson.JSON;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.coupon.entity.CouponAmount;
import com.maxd.modules.coupon.entity.JudgeCouponAmount;
import com.maxd.modules.coupon.service.CouponAmountService;
import com.maxd.modules.coupon.utils.CouponAmountUtil;
import com.maxd.modules.syslog.entity.RequestLogs;
import com.maxd.modules.syslog.service.RequestLogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Date;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/13 14:40
 * @Description:
 */
@RestController
@Api(value="用户优惠金额管理",tags={"用户优惠金额管理"})
@RequestMapping(value = "/couponAmount")
public class CouponAmountController {

    private final static Logger log = LoggerFactory.getLogger(CouponAmountController.class);

    @Autowired
    private CouponAmountService couponAmountService;

    @Autowired
    private RequestLogsService requestLogsService;



    CouponAmountUtil couponAmountUtil = new CouponAmountUtil();

    /**
     * 分页查询用户优惠金额列表
     * @param page
     * @param size
     * @return  返回是分 需要前端去转元
     */
    @RequestMapping(value = "/findAllCouponAmount", method = RequestMethod.POST)
    @ApiOperation("分页查询用户优惠金额列表")
    //@IgnoreSecurity
    public Result findAllCouponAmount(@RequestBody CouponAmount couponAmount, @RequestParam Integer page, @RequestParam int size) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "findAllCouponAmount",null,
                "/couponAmount/findAllCouponAmount", JSON.toJSONString(couponAmount));
        requestLogsService.saveRequestLogs(requestLogs);

        CouponAmount couponAmount1 = couponAmountUtil.copyNull(couponAmount);
        Pageable pageable = PageRequest.of(page, size);
        return couponAmountService.findAll(couponAmount1,pageable);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/addCouponAmount", method = RequestMethod.POST)
    @ApiOperation("新增")
    //@IgnoreSecurity
    public Result addCouponAmount(@RequestBody CouponAmount couponAmount) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "addCouponAmount",null,
                "/couponAmount/addCouponAmount", JSON.toJSONString(couponAmount));
        requestLogsService.saveRequestLogs(requestLogs);

        if(couponAmount==null){
            return ResultUtil.error(601,"新增失败,请稍后再试");
        }
        try {
            couponAmountService.addCouponAmount(couponAmount);
        }catch (Exception e){
            return ResultUtil.error(602,"新增失败,请稍后再试或联系管理员");
        }

        return ResultUtil.success("新增成功");
    }


    /**
     * 根据id修改
     */
    @RequestMapping(value = "/updateCouponAmount", method = RequestMethod.PUT)
    @ApiOperation("根据id修改")
    //@IgnoreSecurity
    public Result updateCouponAmount(@RequestBody CouponAmount couponAmount) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "updateCouponAmount",null,
                "/couponAmount/updateCouponAmount", JSON.toJSONString(couponAmount));
        requestLogsService.saveRequestLogs(requestLogs);

        if(couponAmount==null){
            return ResultUtil.error(601,"修改失败,请稍后再试");
        }
        try {
            couponAmountService.updateCouponAmountById(couponAmount);
        }catch (Exception e){
            return ResultUtil.error(602,"修改失败,请稍后再试或联系管理员");
        }

        return ResultUtil.success("修改成功");
    }



    /**
     * 根据id删除
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation("删除用户优惠金额")
    //@IgnoreSecurity
    public Result deleteById(@RequestParam Long id) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "deleteById",null,
                "/couponAmount/deleteById", String.valueOf(id));
        requestLogsService.saveRequestLogs(requestLogs);

        couponAmountService.deleteById(id);
        return ResultUtil.success();
    }



    /**
     * 根据ids批量删除
     */
    @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    @ApiOperation("删除多个用户优惠金额")
    //@IgnoreSecurity
    public Result deleteByIdList(@RequestParam String ids) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "deleteByIdList",null,
                "/couponAmount/deleteByIdList", String.valueOf(ids));
        requestLogsService.saveRequestLogs(requestLogs);

        couponAmountService.deleteByIdList(ids);
        return ResultUtil.success();
    }


    /**
     *  判断用户抵扣金额是否够用 否则 false
     */
    @RequestMapping(value = "/judgeCouponAmount", method = RequestMethod.POST)
    @ApiOperation("判断优惠券金额是否够用")
    //@IgnoreSecurity
    public Result judgeCouponAmount(@RequestBody JudgeCouponAmount judgeCouponAmount) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "judgeCouponAmount",null,
                "/couponAmount/judgeCouponAmount", JSON.toJSONString(judgeCouponAmount));
        requestLogsService.saveRequestLogs(requestLogs);

        if(judgeCouponAmount==null){
            return ResultUtil.error(603,"userId is not null");
        }
        if(judgeCouponAmount.getCouponAmount()==null){
            return ResultUtil.error(603,"couponAmount is not null");
        }

        CouponAmount couponAmountByUserId = couponAmountService.getCouponAmountByUserId(judgeCouponAmount.getUserId());
        if(couponAmountByUserId==null){
            return ResultUtil.error(603,"额度不足");
        }

        //元转分
        BigDecimal yuan = new BigDecimal("100");

        //判断订单渠道 orderChannel

        BigDecimal bigDecimal = new BigDecimal(judgeCouponAmount.getCouponAmount());
        BigDecimal couponAmountDecimal = bigDecimal.multiply(yuan);
        BigDecimal UuerDeductQuota = new BigDecimal(couponAmountByUserId.getUserDeductQuota());
        BigDecimal subtract = UuerDeductQuota.subtract(couponAmountDecimal);
        int fushu =subtract.compareTo(BigDecimal.ZERO);
        if(fushu==-1){
            return ResultUtil.error(604,"抵扣额度不足");
        }
        return ResultUtil.success();
    }



    /**
     * 根据用户id查询用户抵扣金额 冻结金额
     */
    @RequestMapping(value = "/findCouponAmountByUserId", method = RequestMethod.POST)
    @ApiOperation("判断优惠券金额是否够用")
    public Result findCouponAmountByUserId(@RequestParam String userId) {
        /**添加请求日志*/
        RequestLogs requestLogs = new RequestLogs();
        requestLogs.saveRequestLogs(new java.util.Date(),new java.util.Date(),
                "findCouponAmountByUserId",null,
                "/couponAmount/findCouponAmountByUserId", userId);
        requestLogsService.saveRequestLogs(requestLogs);

        if (userId == null) {
            return ResultUtil.error(603, "userId is not null");
        }

        CouponAmount couponAmountByUserId = couponAmountService.getCouponAmountByUserId(userId);
        if(couponAmountByUserId==null){
            return ResultUtil.error(603,"请兑换卡券");
        }

        return ResultUtil.success(couponAmountByUserId);
    }
}
